//
//  MSCityPickerResultController.m
//  MSJD
//
//  Created by 箭军刘 on 2018/11/10.
//  Copyright © 2018 xiangbo. All rights reserved.
//

#import "MSCityPickerResultController.h"

#import "LHCity.h"

@interface MSCityPickerResultController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) UISearchBar *searchBar;

@property (nonatomic, copy) NSArray *dataSource;

@end

@implementation MSCityPickerResultController

+ (instancetype)loadCityPickerResultController {
    MSCityPickerResultController *cityPickerResultVC = [[MSCityPickerResultController alloc] init];
    return cityPickerResultVC;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.bottom.right.offset(0);
    }];
}

#pragma mark - UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"searchResultCell"];
    
    if (indexPath.row < self.dataSource.count) {
        LHCity *city = self.dataSource[indexPath.row];
        cell.textLabel.text = [MSSafeObject stringInfo:city.name];
        cell.textLabel.font = [UIFont systemFontOfSize:12];
    }
    
    return cell;
}

#pragma mark - UITableViewDelegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.row < self.dataSource.count) {
        LHCity *city = self.dataSource[indexPath.row];
        if (self.SelectSearchResultItemBlock) {
            self.SelectSearchResultItemBlock(city);
        }
    }
}

#pragma mark - Public Method

- (void)refreshWithData:(NSArray *)data {
    // 更新数据源
    self.dataSource = data;
    [self.tableView reloadData];
}

#pragma mark - Getters

- (UISearchBar *)searchBar {
    if (!_searchBar) {
        _searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0,CGRectGetWidth(self.view.frame), 40)];
        _searchBar.delegate = self;
        [_searchBar sizeToFit];
        _searchBar.layer.borderWidth = 1;
        _searchBar.layer.borderColor = [UIColor colorWithHexString:@"0xf5f5f5"].CGColor;
        
    }
    return _searchBar;
}

- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.rowHeight = 30.0;
        
        [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"searchResultCell"];
        
        _tableView.tableHeaderView = self.searchBar;
    }
    return _tableView;
}

#pragma mark -  Memory Method

- (void)dealloc {
    NSLog(@"%s", __func__);
}

@end
